package com.fyzk.activity.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.app.AppToast;
import com.fyzk.base.BaseFragment;
import com.fyzk.bean.AccountMoneyResponse;
import com.fyzk.bean.TransFormResponse;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.dialog.TransformDialog;
import com.fyzk.net.Commands;
import com.fyzk.net.PublicMethod;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * <p>
 *
 * @author 指尖沙漏
 * @mood
 * @description
 * @date 2019/9/23 16:11
 * </p>
 */
@ContentView(R.layout.fg_transform)
public class TransformFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "param1";

    @ViewInject(R.id.transformFrag_tvUserName)
    private TextView tvUserName;
    @ViewInject(R.id.transformFrag_tvAllow)
    private TextView tvAllow;
    @ViewInject(R.id.transformFrag_etMoney)
    private EditText etMoney;
    @ViewInject(R.id.transformFrag_tvOk)
    private TextView tvOk;
    @ViewInject(R.id.transformFrag_tvInfo)
    private TextView tvInfo;

    @ViewInject(R.id.all)
    private TextView all;

    private double keTiMoney = 0;
    private String userName;
    private String transContent;
    private AccountMoneyResponse accountMoneyResponse;

    public static TransformFragment newInstance(String userName) {
        TransformFragment fragment = new TransformFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, userName);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            userName = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        userName = MySharedPreferences.getInstance().getUserName();
        transContent = MySharedPreferences.getInstance().getHandselContent();
        initView();
    }

    private void initView() {
        tvUserName.setText(userName);
        tvInfo.setText(transContent);
        tvOk.setOnClickListener(this);
        all.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        //请求获取彩金
        requestLeftMoney();
    }

    //查询账户彩金
    private void requestLeftMoney() {
        httpPostJson(CommandUtil.ACCOUNT_MONEY, Commands.ACCOUNT_MONEY, null, true);
    }

    private void requestTransform() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("drawAmount", transformMoney);
            httpPostJson(CommandUtil.TRANSFORM, Commands.TRANSFORM, request_json, true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.ACCOUNT_MONEY:
                accountMoneyResponse = JSON.parseObject(resultBean.getData(), AccountMoneyResponse.class);
                keTiMoney = accountMoneyResponse.getDrawbalance();
                oldMoney = accountMoneyResponse.getBalance();
//                tvAllow.setText(Html.fromHtml("可转换彩金：<font color='#5C96F9'>" + PublicMethod.formatBalance(keTiMoney) + "米</font>"));
                tvAllow.setText("可转换彩金：" + PublicMethod.formatBalance(keTiMoney) + "米");
                break;
            case CommandUtil.TRANSFORM:
                TransFormResponse response = JSON.parseObject(resultBean.getData(), TransFormResponse.class);
                newMoney = response.getBalance();
                transformMoney = response.getDrawAmount();
                showTransformDialog();
                break;
        }
    }

    private long transformMoney = 0;
    private long oldMoney = 0;
    private long newMoney = 0;

    private void showTransformDialog() {
        TransformDialog mDialog = new TransformDialog(getContext(), new TransformDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    AppToast.getToast().show("彩金转换成功");
                    getActivity().finish();
                }
            }
        }, transformMoney, oldMoney, newMoney);
        mDialog.setTitle("彩金转换成功");
        mDialog.show();
    }

    @Override
    public void onFailure(int requestCode, String errorCode, String message) {
        cancelProgressDialog();
        switch (requestCode) {
            case CommandUtil.TRANSFORM:
                showToast(message);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.transformFrag_tvOk:
                if (et_yanZheng()) {
                    transformMoney = Long.parseLong(etMoney.getText().toString() + "00");
                    requestTransform();
                }
                break;
            case R.id.all:
                try {
                    etMoney.setText(""+new Double(PublicMethod.formatBalance(keTiMoney)).intValue());
                }catch (Exception e){
                }
                break;
        }
    }

    /**
     * 非空验证
     */
    private boolean isNull(EditText editText) {
        if (editText.getText() == null || editText.getText().toString().equals("")) {
            editText.requestFocus();
            return true;
        }
        return false;
    }

    /**
     * 输入框验证
     *
     * @return
     */
    private boolean et_yanZheng() {
        if (isNull(etMoney)) {
            AppToast.getToast().show("转换彩金不能为空");
            return false;
        }

        if (Double.parseDouble(etMoney.getText().toString()) * 100 == 0) {
            AppToast.getToast().show("转换彩金需大于0元");
            return false;
        }

        if (Double.parseDouble(etMoney.getText().toString()) * 100 > keTiMoney) {
            AppToast.getToast().show("可转换彩金不足");
            return false;
        }

        return true;
    }

}
